﻿
(function () {
    appModule.controller('tenant.views.announcement.users', [
        '$scope', '$uibModalInstance', 'abp.services.app.user','appSession',
        function ($scope, $uibModalInstance, userService,appSession) {
            var vm = this;

            vm.saving = false;
            vm.announcement = null;


            vm.cancel = function () {
                $uibModalInstance.dismiss();
            };
            vm.checkedIds = {};
            function onDataBound(e) {
                var view = this.dataSource.view();
                for (var i = 0; i < view.length; i++) {
                    if (vm.checkedIds[view[i].id]) {
                        var row = this.tbody.find("tr[data-uid='" + view[i].uid + "']");
                        var rowRelated = vm.mainGrid._relatedRow(row);
                        row.addClass("k-state-selected");
                        rowRelated.addClass("k-state-selected");
                        rowRelated.find(".checkbox").attr("checked", "checked");
                    }
                }
            }
            var urlUser = "/api/services/app/user/";

            var mainGridDataSource = new kendo.data.DataSource({
                type: 'aspnetmvc-ajax',
                transport: {
                    read: {
                        url: urlUser + "KendoGetUsers",
                        contentType: "application/json",
                        type: "POST"
                    },
                    parameterMap: function (options, operation) {
                        return JSON.stringify(options);
                    }
                },
                schema: {
                    data: function (data) {
                        if (data.success === true && data.result != null) {
                            return data.result.data;
                        }
                        return {};
                    },
                    total: function (data) {
                        if (data.success === true && data.result != null) {
                            return data.result.total;
                        }
                        return 0;
                    },
                    model: {
                        id: "id",
                        fields: {
                            id: { type: "number" },
                        }
                    }
                },
                filter: {
                    logic: "and",
                    filters: [
                         { field: "id", operator: "neq", value: appSession.user.id }
                    ]
                }
            });

            vm.mainGridOptions = {
                dataSource: mainGridDataSource,
                dataBound: onDataBound,
                columnMenu: true,
                reorderable: true,
                resizable: true,
                filterable: true,
                scrollable: true,
                selectable: "multiple",
                allowCopy: true,
                //height: 650,
                change: function (e) {
                    var selectedRows = this.select();
                    for (var i = 0; i < selectedRows.length; i++) {
                        vm.curAnnouncement = this.dataItem(selectedRows[i]);
                    }
                },
                sortable: {
                    mode: "multiple"
                },
                messages: {
                    commands: {
                        excel: "导出"
                    }
                },
                columns: [
                     {
                         title: "<input id='checkAll' type='checkbox' ng-model='vm.allCheck' ng-click='vm.checkAll()'/>",
                         template: "<input type='checkbox' class='checkbox' ng-click='vm.checkOne($event)'/>", width: 30,
                     },
                    {
                        title: '员工姓名',
                        field: 'surname',
                        template: '<span>#=surname##=name#</span>'
                    },
                    {
                        title: '主编 ',
                        field: 'isEditor',
                        width: 80,
                        template: '<div class=\"ui-grid-cell-contents\">' +
                        '  <span ng-show="#=isEditor#" class="label label-success">' + app.localize('Yes') + '</span>' +
                        '  <span ng-show="!#=isEditor#" class="label label-default">' + app.localize('No') + '</span>' +
                        '</div>',
                    },
                ]
            };

            vm.checkOne = function (e) {
                var element = $(e.currentTarget);
                var checked = element.is(':checked');
                var row = element.closest("tr");
                var dataItem = vm.mainGrid.dataItem(row);

                var rowRelated = vm.mainGrid._relatedRow(row);

                vm.checkedIds[dataItem.id] = checked;

                if (checked) {
                    row.addClass("k-state-selected");
                    rowRelated.addClass("k-state-selected");
                } else {
                    row.removeClass("k-state-selected");
                    rowRelated.removeClass("k-state-selected");
                }

            };

            vm.checkAll = function () {
                var view = vm.mainGrid.dataSource.view();
                var checked = vm.allCheck;
                for (var i = 0; i < view.length; i++) {
                    var row = vm.mainGrid.tbody.find("tr[data-uid='" + view[i].uid + "']");
                    var rowRelated = vm.mainGrid._relatedRow(row);

                    var dataItem = vm.mainGrid.dataItem(row);
                    vm.checkedIds[dataItem.id] = checked;

                    if (checked) {
                        row.addClass("k-state-selected");
                        rowRelated.addClass("k-state-selected");
                        rowRelated.find(".checkbox").attr("checked", "checked");
                    } else {
                        row.removeClass("k-state-selected");
                        rowRelated.removeClass("k-state-selected");
                        rowRelated.find(".checkbox").attr("checked", false);
                    }
                }
            };
            vm.obj = new Object();
            vm.obj.userStr = "";
            vm.obj.ids = [];
            vm.save = function () {
                var view = vm.mainGrid.dataSource.view();
                vm.obj.userStr = "";
                for (var id in vm.checkedIds) {
                    if (vm.checkedIds[id]) {
                        vm.obj.ids.push(id);
                        for (var i = 0; i < view.length; i++) {
                            if (parseInt(id) == parseInt(view[i].id)) {
                                vm.obj.userStr = view[i].surname + view[i].name + ";" + vm.obj.userStr;
                            }
                        }
                    }
                }
                $uibModalInstance.close(vm.obj);
            };
        }
    ]);
})();
